package com.chaoyong.higo.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.bean.Comments;
import com.chaoyong.higo.utils.ViewHolder;
import com.chaoyong.higo.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommnetsAdapter extends BaseAdapter{

	private Activity act;
	private List<Comments> datas;
	private LayoutInflater inflater;
	
	private RoundImageView photo;
	private TextView username,time,contents;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	
	public CommnetsAdapter(Activity act, List<Comments> datas) {
		// TODO Auto-generated constructor stub
		this.act = act;
		this.datas = datas;
		inflater = LayoutInflater.from(act);
	}
	
	public void changeDatas(List<Comments> datas) {
		// TODO Auto-generated method stub
		this.datas = datas;
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_comments, null);
		}
		photo = ViewHolder.get(convertView, R.id.item_comments_photo);
		username = ViewHolder.get(convertView, R.id.item_comments_username);
		time = ViewHolder.get(convertView, R.id.item_comments_time);
		contents = ViewHolder.get(convertView, R.id.item_comments_contents);
		
		ImageLoader.getInstance().displayImage(datas.get(position).getImg(), photo);
		username.setText(datas.get(position).getSend_user_name());
		time.setText(sdf.format(new Date(Long.parseLong(datas.get(position).getAdd_time()) * 1000L)));
		contents.setText(datas.get(position).getContents());
		
		return convertView;
	}

}
